<template>
  <a-modal
    title="新增订单物料"
    :width="1200"
    :maskClosable="false"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item v-show="false"
          label="订单_ID"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入订单_ID" v-decorator="['orderId', {rules: [{required: true, message: '请输入订单_ID！'}]}]" />
        </a-form-item>

        <a-form-item
          label="物料ID"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input v-show="false" placeholder="请输入物料ID" v-decorator="['materialId', {rules: [{required: true, message: '请输入物料ID！'}]}]" />
          <a-button type="dashed"
                    style="width: 100%;"
                    @click="$refs.emsMaterialIndex1.show({materialType})">
            {{ material.materialCode || '选择原料' }}
          </a-button>
        </a-form-item>

        <a-form-item
          label="需求数"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入需求数" v-decorator="['number', {rules: [{required: true, message: '请输入需求数！'}]}]" />
        </a-form-item>

      </a-form>
    </a-spin>

    <ems-material-index1 ref="emsMaterialIndex1" :on-row-selected="onEmsMaterialIndex1Selected" :show-material-type="materialType!=$businessEnum.MATERIAL_TYPE_ENUM.packing.id"/>
  </a-modal>
</template>

<script>
  import { add } from '@/api/modular/main/emsordermaterial/emsOrderMaterialManage'
  import { EXECUTE_STATUS_ENUM, MATERIAL_TYPE_ENUM } from '@/utils/util'
  import emsMaterialIndex1 from '../emsmaterial/emsMaterialIndex1'

  export default {
    components: {
      emsMaterialIndex1
    },
    data() {
      return {
        labelCol: {
          xs: {span: 24},
          sm: {span: 5}
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 15}
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        record: {},
        material: {},
        materialType: null,
      }
    },
    methods: {
      add(record, materialType) {
        this.visible = true
        this.record = record
        this.materialType = materialType
        setTimeout(() => {
          this.form.setFieldsValue(record);
        }, 100)
      },
      handleSubmit() {
        const {form: {validateFields}} = this
        this.confirmLoading = true

        validateFields((errors, values) => {
          if (!errors) {
            values = Object.assign({
              orderMaterialId: '',
              orderId: '',
              sourceOrderId: '0',
              sourceOrderMaterialId: '0',
              bomId: '0',
              materialId: '',
              number: '',
              extraNumber: '0',
              completedNumber: '0',
              executeStatus: EXECUTE_STATUS_ENUM.IN_PROGRESS.id,
              executeProgress: '0',
              file: '',
              remark: '',
              markAbnormal: '0',
              status: '1',
              createTime: '',
              createUser: '',
              updateTime: '',
              updateUser: ''
            }, values)
            add(values).then((res) => {
              this.confirmLoading = false
              if (res.success) {
                this.$message.success('新增成功')
                this.handleCancel()
                this.$emit('ok', values)
              } else {
                this.$message.info('新增失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel() {
        this.form.resetFields();
        this.material = {}
        this.visible = false
      },
      onEmsMaterialIndex1Selected(materialRow) {
        let {materialId, materialName, materialCode, materialType, costPrice} = materialRow
        this.material = materialRow
        this.form.setFieldsValue(this.material);
      }
    }
  }
</script>
